
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LinkedListVsArrayList {

	private static final Integer ELEMENT = 42;

	public static void main(String[] args) {
	
		List<Integer> arrayList = new ArrayList<>();
		List<Integer> linkedList = new LinkedList<>();
		
		
		System.out.println("WarmUp");
		for (int i = 0; i < 20000; i++) {
			getAverageTime(arrayList);
			getAverageTime(linkedList);
		}
		arrayList.clear();
		linkedList.clear();
		
		
		System.out.println("sizeOfList   ArrayList   LinkedList");
		for (int i = 0; i < 3000; i++) {
			if (i % 10 == 0) {
				System.out.printf("%8d : %8d %8d\n", i, getAverageTime(arrayList), getAverageTime(linkedList));
			}
			linkedList.add(0, ELEMENT );			
			arrayList.add(0, ELEMENT );			
		}
	}


	public static long getAverageTime(List<Integer> list) {
		final int NCYCLES = 1000; 
		final int NTIMES = 10;
		long sum=0;

		for (int i = 0; i < NCYCLES; i++) {
			
			long startTime = System.nanoTime();
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );
			list.add(0, ELEMENT );			
			long elapsed = ( System.nanoTime() - startTime ) / NTIMES;
			
			sum += elapsed;
			
			for (int j = 0; j < NTIMES; j++) {
				list.remove(0);
			}
		}
		return sum / NCYCLES;
	}
	
}
